from datetime import datetime
import os
import random
import string

from fastapi import File, UploadFile


class Utils:
    def __init__(self):
        pass


def activation_code(id, length=10):
    """
    id + L + 随机码
    string模块中的3个函数:string.letters,string.printable,string.printable
    """
    prefix = hex(int(id))[2:] + "L"
    length = length - len(prefix)
    chars = string.ascii_letters + string.digits
    return prefix + "".join([random.choice(chars) for i in range(length)])


def get_id(code):
    """Hex to Dec"""
    id_hex = code.split("L")[0]
    return str(int(id_hex, 16))


def getDate():
    now = datetime.now()
    return now.strftime("%Y%m%d")


async def saveFile(path, filename, file: UploadFile = File(...)):
    if not os.path.exists(path):
        os.makedirs(path)
    file_path = os.path.join(path, filename)
    with open(file_path, "wb") as buffer:
        content = await file.read()
        buffer.write(content)
    return file_path
